$(function () {
    // //1点击标题复选框表示全选
    // $("#mec-head-checkbox").on("click", function () {
    //     // alert("执行通过账号找回密码的方法");
    //     // alert("密码是123");
    //     judgement();
    // });
    //
    // //实现全选
    // function judgement() {
    //     var isChecked = $("#mec-head-checkbox").prop("checked");
    //     if (isChecked == true) {
    //         // $("#mec-head-checkbox").on("click",function () {
    //         //     judgement();
    //         $("#mec-content-checkbox").prop("checked", true);
    //         //});
    //     }
    //     else {
    //         //  $("#mec-head-checkbox").on("click",function () {
    //         //     judgement();
    //         $("#mec-content-checkbox").prop("checked", false);
    //         // });
    //     }
    // }

    //新增信息界面隐藏显示进货信息界面
    $(".stock-input").css("display", "none");

    //2监听新增信息按钮
    $("#stock-add").click(function () {
        $(".stock-table").css("display", "none");
        $(".stock-input").css("display", "block");

        //获取类别
        $("#select").html("");
        $("#select").append("<option value=''>请选择药品类别</option>");
        // $("#select").append("<option value = '1'>" + '内科' + "</option>");
        $.ajax({
            url: "/hospital/medicine_type/all.action",
            dataType: "json",
            type: "GET",
            success: function (data) {
                $.each(data, function (index, ele) {
                    $("#select").append("<option value = '" + ele.mdc_type_id + "'>" + ele.mdc_type_name + "</option>");
                });
            },
            error: function () {
                alert("mdc type error");
            }
        });
        //获取供应商
        $("#select1").html("");
        $("#select1").append("<option value=''>请选择药品供应商</option>");
        $.ajax({
            url: "/hospital/suppiler/all_suppiler.action",
            dataType: "json",
            type: "GET",
            success: function (data) {
                $.each(data, function (index, ele) {
                    $("#select1").append("<option value = '" + ele.id + "'>" + ele.sup_name + "</option>");
                });
            },
            error: function () {
                alert("mdc sup error");
            }
        });

    });


    //新增信息界面 确认按钮
    $("#addStock-sure").on("click", function () {
        //获取当前输入数据
        var num = $("#mec-num").val();
        var name = $("#mec-name").val();
        var unit = $("#mec-unit").val();
        var spec = $("#mec-spec").val();
        var typeId = $("#select option:selected").val();
        // $("#select").change(function () {
        //     var typeId = $(this).children("option:selected").val();
        //     console.log(typeId);
        // });
        var supId = $("#select1 option:selected").val();
        var company = $("#mec-company").val();
        var date = $("#mec-time").val();
        var price = $("#mec-price").val();
        var guaranteeDate = $("#select2 option:selected").val();
        var count = $("#mec-count").val();
        var total = $("#mec-total").val();

        //发送数据到后台添加记录
        $.ajax({
            url: "/hospital/purchase/add.action",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                "mdcNum": num, "mdcName": name, "mdcUnit": unit, "mdcSpec": spec,
                "mdcTypeId": typeId, "mdcSupplierId": supId, "mdcCompany": company, "pchDate": date,
                "pchPrice": price, "pchGuaranteeDate": guaranteeDate, "pchCount": count, "pchTotal": total
            }),
            success: function (data) {//data显示为 是否插入成功，直接弹出 key：info，value：成功|失败
                alert("添加成功");
                var $list = $("#tb_2");
                //1.清空表格数据
                $("#tb_2 tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
                $(".stock-input").css("display", "none");
                $(".stock-table").css("display", "block");
            },
            error: function () {
                alert("请求数据失败");
            }
        });
    });

    //新增信息界面 输入框失去焦点事件
    $("#mec-num").on("blur", function () {
        var mecNumResult = $("#mec-num").val();
        if ($.trim(mecNumResult).length == 0) {
            $("#judgeNum").html("/*药品编号不能为空*/");
        }
        else {
            $("#judgeNum").remove();
        }
    });
    $("#mec-name").on("blur", function () {
        var mecNameResult = $("#mec-name").val();
        if ($.trim(mecNameResult).length == 0) {
            $("#judgeName").html("/*药品名称不能为空*/");
        }
        else {
            $("#judgeName").remove();
        }
    });
    $("#mec-unit").on("blur", function () {
        var mecUnitResult = $("#mec-unit").val();
        if ($.trim(mecUnitResult).length == 0) {
            $("#judgeUnit").html("/*药品单位不能为空*/");
        }
        else {
            $("#judgeUnit").remove();
        }
    });
    $("#mec-count").on("blur", function () {
        var mecCountResult = $("#mec-count").val();
        var $count = /^[0-9]*$/;
        if ($.trim(mecCountResult).length == 0) {
            $("#judgeCount").html("/*进药数量不可为空）*/");
        }
        else if (!$count.test(mecCountResult)) {
            $("#judgeCount").html("/*请输入正确的数字*/");
        }
        else {
            $("#judgeCount").remove();
        }
    });
    $("#mec-spec").on("blur", function () {
        var $mecSpecResult = $("#mec-spec").val();
        if ($.trim($mecSpecResult).length == 0) {
            $("#judgeSpec").html("/*药品规格不能为空*/");
        } else {
            $("#judgeSpec").remove();
        }
    });
    $("#mec-company").on("blur", function () {
        var $mecCompanyResult = $("#mec-company").val();
        if ($.trim($mecCompanyResult).length == 0) {
            $("#judgeCompany").html("/*生产厂家不能为空*/");
        } else {
            $("#judgeCompany").remove();
        }
    });
    $("#mec-price").on("blur", function () {
        var $mecPriceResult = $("#mec-price").val();
        if ($.trim($mecPriceResult).length == 0) {
            $("#judgePrice").html("/*进药价格不能为空*/");
        }
        else {
            $("#judgePrice").remove();
        }
    });

    //新增信息界面 返回按钮点击事件
    $("#addStock-back").click(function () {
        $(".stock-input").css("display", "none");
        $(".stock-table").css("display", "block");
    });
    //新增信息界面 获取总价按钮点击事件
    $("#stock-get-total").click(function () {
        var price = $("#mec-price").val();
        var count = $("#mec-count").val();
        var total = price * count;
        $("#mec-total").val(total);
    });

    //进货信息界面 查询按钮点击事件
    $("#stockNameSelectBtn").click(function () {
        var mdcName = $("#nameInput").val();
        $.ajax({
            url: "/hospital/purchase/med_name.action",
            dataType: "json",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"mdcName": mdcName}),
            success: function (data) {
                var $list = $("#tb_2");
                //1.清空表格数据
                $("#tb_2 tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
            },
            error: function () {
                alert("请求数据失败");
            }
        });
    });
    //进货信息界面 查询按钮点击事件
    $("#stockSuiSelectBtn").click(function () {
        var sup = $("#suiInput").val();
        $.ajax({
            url: "/hospital/purchase/sup_name.action",
            dataType: "json",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"supName": sup}),
            success: function (data) {
                var $list = $("#tb_2");
                //1.清空表格数据
                $("#tb_2 tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
            },
            error: function () {
                alert("请求数据失败");
            }
        });
    });
    //进货信息界面 查询按钮点击事件
    $("#stockDateSelectBtn").click(function () {
        var date = $("#dateInput").val();
        $.ajax({
            url: "/hospital/purchase/date.action",
            dataType: "json",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"pchDate": date}), //进药时间
            contentType: "application/json;charset=utf-8",
            success: function (data) {
                var $list = $("#tb_2");
                //1.清空表格数据
                $("#tb_2 tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
            },
            error: function () {
                alert("请求数据失败");
            }
        });
    });
    //隐藏表格的药品id
    // $("#tb_2").delegate("tr",find("td:eq(0)").hide());
    // $("tr").find("td:eq(0)").hide();

    //动态创建
    getDataList1();

    //创建进货信息记录表格数据
    function getDataList1() {
        $.ajax({
            type: "GET",
            url: "/hospital/purchase/all.action",
            dataType: "json",
            success: function (data) {
                var $list = $("#tb_2");
                // console.log($list);
                // console.log(data);
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
            },
            error: function (jqXHR) {
                alert(jqXHR.state());
            }
        });
    }

    //动态创建一行表单数据
    function createDataItem(index, data) {
        var $item1 = $("" + "<tr>\n" +
            // "            <td style=\"display: none\">"+data.pchMdcId+"</td>\n" +
            "            <td style=\"width:100px;\">" + data.id + "</td>\n" +
            "            <td >" + data.mdcName + "</td>\n" +
            "            <td>" + data.mdcUnit + "</td>\n" +
            "            <td>" + data.mdcSpec + "</td>\n" +
            "            <td>" + data.supName + "</td>\n" +
            "            <td>" + data.pchPrice + "</td>\n" +
            "            <td>" + data.pchCount + "</td>\n" +
            "            <td>" + data.pchTotal + "</td>\n" +
            "            <td>" + data.pchDate + "</td>\n" +
            "            <td>" + data.pchGuaranteeDate + "</td>\n" +
            "            <td>\n" +
            "                <button class='stock-table-update-btn'>编辑</button>\n" +
            "                <button class='stock-table-cancel-btn'>删除</button>\n" +
            "            </td>\n" +
            "        </tr>");
        return $item1;
    }

    //操作栏 删除按钮事件委托
    $("#tb_2").delegate(".stock-table-cancel-btn", "click", function () {
        //获取当前行 进货记录id ---pchId
        var id = $(this).parents("tr")[0].children[0].innerHTML;
        $.ajax({
            url: "/hospital/purchase/delete.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"pchId": id}),
            success: function (data) {
                alert("删除成功！");
                //1.返回数据动态更新表格数据
                var $list = $("#tb_2");
                //1.清空表格数据
                $("#tb_2 tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
            },
            error: function () {
                alert("数据请求失败");
            }
        });
    });
// alert("1")
    //操作栏 编辑按钮事件委托
    $("#tb_2").delegate(".stock-table-update-btn", "click", function () {
        var $hah = $(this).parents("tr")[0];
        // var num0 = $hah.children[0].innerHTML;
        // console.log(num0);
        var num1 = $hah.children[0].innerHTML;
        var num2 = $hah.children[1].innerHTML;
        var num3 = $hah.children[2].innerHTML;
        var num4 = $hah.children[3].innerHTML;
        var num5 = $hah.children[4].innerHTML;
        var num6 = $hah.children[5].innerHTML;
        var num7 = $hah.children[6].innerHTML;
        var num8 = $hah.children[7].innerHTML;
        var num9 = $hah.children[8].innerHTML;
        var num10 = $hah.children[9].innerHTML;
        $("#stock-update-input-num").val(num1);
        $("#stock-update-input-name").val(num2);
        $("#stock-update-input-unit").val(num3);
        $("#stock-update-input-type").val(num4);
        $("#stock-update-input-sui").val(num5);
        $("#stock-update-input-price").val(num6);
        $("#stock-update-input-count").val(num7);
        $("#stock-update-input-total").val(num8);
        $("#stock-update-input-data").val(num9);
        $("#stock-update-input-useData").val(num10);
        $(".all-stock-update-input").css("display", "block");
        $("#over").css("display", "block");

        //编辑弹窗 确认按钮
        $("#addStock-update-sure").unbind("click").click(function () {
            var pchId = $("#stock-update-input-num").val();
            // var mdcId = $("").val();
            var price = $("#stock-update-input-price").val();
            var count = $("#stock-update-input-count").val();
            var total = price * count;
            var date = $("#stock-update-input-data").val();
            var guaranteeDate = $("#stock-update-input-useData").val();
            $.ajax({
                url: "/hospital/purchase/update.action",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "pchId": pchId, "pchPrice": price, "pchCount": count,
                    "pchTotal": total, "pchDate": date, "pchGuaranteeDate": guaranteeDate
                }),
                success: function (data) {  //该处data为所有进货记录
                    alert("修改成功！");
                    //1.返回数据动态更新表格数据
                    var $list = $("#tb_2");
                    //1.清空表格数据
                    $("#tb_2 tr:not(:first)").html("");
                    //2.动态创建数据
                    $.each(data, function (index, ele) {
                        var $item1 = createDataItem(index, ele);
                        $list.append($item1);
                    });
                    //2.隐藏编辑框
                    $(".all-stock-update-input").css("display", "none");
                    $("#over").css("display", "none");
                },
                error: function (jqXHR) {
                    alert("数据请求失败");
                }
            });

        });
    });
    //编辑弹窗返回按钮
    $("#addStock-update-back").click(function () {
        $(".all-stock-update-input").css("display", "none");
        $("#over").css("display", "none");
    });

    // layui.use('laydate', function () {
    //     var laydate = layui.laydate;
    //     //日期时间选择器
    //     laydate.render({
    //         elem: '#dateInput',
    //         type: 'datetime'
    //     });
    // });
    // $("#stockNameSelectBtn").click(function () {
    //    alert("aaaaaa");
    // });

    // $(".stock-select-btn").click(function () {
    //     alert(1242321);
    // });
});